home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / Yenta / Erics C++ Libraries / Interface Classes / CPPStaticText.cp < prev    next >
Encoding:
Text File  |  1996-04-04  |  4.9 KB  |  186 lines  |  [TEXT/KAHL]

  1. /********************************************************* DEFINITION
  2.     DATE:    9/24/93
  3.     AUTHOR: Eric R. Rosé
  4.     
  5.     CLASS:  CPPStaticText
  6.     
  7.     SUPERCLASS: CPPVisualObject
  8.     
  9.         This C++ class manages a static string
  10.     
  11. ********************************************************************/
  12.  
  13. #include <CPPStaticText.h>
  14. #include <CPPWindow.h>
  15. #include <MemoryTools.h>
  16. #include <StringTools.h>
  17.  
  18. /*-----------------------------------------------------------------*/
  19. /*------------------------ PUBLIC METHODS -------------------------*/
  20. /*-----------------------------------------------------------------*/
  21.  
  22.     CPPStaticText::CPPStaticText (CPPWindow *itsWindow, Rect *itsBounds,
  23.                                    int StrID, int Font, int FSize,
  24.                                    int FJust, int FStyle,
  25.                                    RGBColor *FColor, Boolean canBeTarget,
  26.                                    Boolean active, Boolean visible) :
  27.                     CPPVisualObject (itsWindow, itsBounds, canBeTarget,
  28.                                      active, visible)
  29.     {
  30.         StringHandle    tempHandle = GetString(StrID);
  31.         
  32.         if (tempHandle)
  33.           {
  34.               this->itemText = (StringPtr)Hand2Ptr((Handle)tempHandle);
  35.               ReleaseResource((Handle)tempHandle);
  36.           }
  37.         else
  38.           this->itemText = NULL;
  39.           
  40.         MakeStatText (itsBounds, Font, FSize, FJust, FStyle, FColor);
  41.     }
  42.  
  43. /*-----------------------------------------------------------------*/
  44.  
  45.     CPPStaticText::CPPStaticText (CPPWindow *itsWindow, Rect *itsBounds,
  46.                                    StringPtr theString, int Font, 
  47.                                    int FSize, int FJust, 
  48.                                    int FStyle, RGBColor *FColor,
  49.                                    Boolean canBeTarget, Boolean active, 
  50.                                    Boolean visible) :
  51.                     CPPVisualObject (itsWindow, itsBounds, canBeTarget,
  52.                                      active, visible)
  53.     {
  54.         if (theString)
  55.           this->itemText = String2String (theString);
  56.         MakeStatText (itsBounds, Font, FSize, FJust, FStyle, FColor);
  57.     }
  58.  
  59. /*-----------------------------------------------------------------*/
  60.  
  61.     CPPStaticText::~CPPStaticText (void)
  62.     {
  63.         if (this->itemText)
  64.           DisposPtr((Ptr)this->itemText);
  65.     }
  66.  
  67. /*-----------------------------------------------------------------*/
  68.  
  69.     char    *CPPStaticText::ClassName (void)
  70.     {
  71.         return "CPPStaticText";
  72.     }
  73.  
  74. /*-----------------------------------------------------------------*/
  75.  
  76.     void    CPPStaticText::Draw (void)
  77.     {
  78.         RGBColor    OldColor;
  79.         
  80.         if (this->owningWindow && this->itemText && this->IsVisible())
  81.           {
  82.               // save the old values for the font, face, style & color
  83.             short OldFont = this->owningWindow->txFont;
  84.             short OldSize = this->owningWindow->txSize;
  85.             short OldFace = this->owningWindow->txFace;
  86.             
  87.             if (this->InColorWindow())
  88.               {
  89.                 GetForeColor(&OldColor);
  90.                 RGBForeColor(&this->fontColor);
  91.               }
  92.             TextSize (this->fontSize);
  93.             TextFont (this->fontID);
  94.             TextFace (this->fontStyle);
  95.             TextBox (this->itemText+1, *this->itemText, 
  96.                      &this->bounds, this->fontJust);
  97.             
  98.             // restore the old font attributes
  99.             this->owningWindow->txFont = OldFont;
  100.             this->owningWindow->txSize = OldSize;
  101.             this->owningWindow->txFace = OldFace;
  102.             if (this->InColorWindow())
  103.               RGBForeColor(&OldColor);
  104.           }
  105.     }
  106.  
  107. /*-----------------------------------------------------------------*/
  108.  
  109.     void    CPPStaticText::MakeVisible (Boolean nowVisible)
  110.     /* hide or show the text item */
  111.     {
  112.           if (!nowVisible)
  113.             EraseRect (GetBounds());
  114.           InvalRect (GetBounds());
  115.       
  116.         CPPVisualObject::MakeVisible(nowVisible);
  117.     }
  118.  
  119. /*-----------------------------------------------------------------*/
  120.  
  121.     Rect    *CPPStaticText::GetBounds (void)
  122.     {
  123.         return &this->bounds;
  124.     }
  125.  
  126. /*-----------------------------------------------------------------*/
  127.     
  128.     void    CPPStaticText::MoveContent (short newH, short newV)
  129.     {
  130.         OffsetRect (&this->bounds, newH - this->bounds.left,
  131.                     newV - this->bounds.top);
  132.     }
  133.  
  134. /*-----------------------------------------------------------------*/
  135.  
  136.     void    CPPStaticText::ResizeContent (short newWidth, short newHeight)
  137.     {
  138.         this->bounds.right = this->bounds.left+newWidth;
  139.         this->bounds.bottom = this->bounds.top+newHeight;
  140.     }
  141.  
  142.  
  143. /*-----------------------------------------------------------------*/
  144.  
  145.     void    CPPStaticText::SetitsString (StringPtr newString, Boolean keepCopy)
  146.     {
  147.         GrafPtr    SavePort;
  148.         
  149.         GetPort(&SavePort);
  150.         SetPort(this->owningWindow);
  151.         
  152.         if (this->itemText)
  153.           DisposPtr((Ptr)this->itemText);
  154.         if (keepCopy)
  155.           this->itemText = String2String(newString);
  156.         else
  157.           this->itemText = newString;
  158.         
  159.         // force the string to redraw
  160.         EraseRect(GetBounds());
  161.         InvalRect(GetBounds());
  162.         
  163.         SetPort(SavePort);
  164.     }
  165.  
  166. /*-----------------------------------------------------------------*/
  167.  
  168.     StringPtr    CPPStaticText::GetitsString (void)
  169.     {
  170.         return this->itemText;
  171.     }
  172.  
  173. /*-----------------------------------------------------------------*/
  174.  
  175.     void    CPPStaticText::MakeStatText (Rect *itsBounds, short Font, 
  176.                                          short FSize, short FJust, 
  177.                                          short FStyle,
  178.                                          RGBColor *FColor)
  179.     {
  180.         this->bounds = *itsBounds;
  181.         this->fontID = Font;
  182.         this->fontSize = FSize;
  183.         this->fontJust = FJust;
  184.         this->fontStyle = FStyle;
  185.         this->fontColor = *FColor;
  186.     }